package com.android.contentprovidersqlb;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.jeremyliao.liveeventbus.LiveEventBus;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button button1;
    private Button button2;
    private Button button3;
    private Button button4;
    //创建期待匹配的uri
    private Uri uri1 = Uri.parse("content://com.android.lmy.DatabaseProvider/student");
    private List<Student> studentList = new ArrayList<>();


    private BroadcastReceiver receiveBroadCast;
    private BroadcastReceiver receiveBroadCast2;

    private IntentFilter filter, filter2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        button3 = findViewById(R.id.button3);
        button4 = findViewById(R.id.button4);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);

        LiveEventBus
                .get("key_name", String.class)
                .observe(this, new Observer<String>() {
                    @Override
                    public void onChanged(@Nullable String s) {
                        Log.e("LiveEventBus", s);
                    }
                });

    }


    @Override
    protected void onResume() {
        super.onResume();
        // 注册广播接收
        receiveBroadCast = new ReceiveBroadCast();
        filter = new IntentFilter();
        filter.addAction("com.android.contentprovidersqlb"); // 只有持有相同的action的接受者才能接收此广播
        Log.e("TTT", "注册广播接收");
        registerReceiver(receiveBroadCast, filter);
        // 注册广播接收
        receiveBroadCast2 = new ReceiveBroadCast2();
        filter2 = new IntentFilter();
        filter2.addAction("com.android.contentprovidersqlb"); // 只有持有相同的action的接受者才能接收此广播
        Log.e("TTT", "注册广播接收");
        registerReceiver(receiveBroadCast2, filter2);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(receiveBroadCast);
        unregisterReceiver(receiveBroadCast2);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1:

                Intent intent = new Intent("com.android.contentprovidersqlb");
                intent.setComponent(new ComponentName("com.android.contentprovidersqlb", "com.android.contentprovidersqlb.ReceiveBroadCast"));
                sendBroadcast(intent);

//                ContentValues contentValues1s = new ContentValues();
//                contentValues1s.put("name", "小明");
//                contentValues1s.put("sex", "女");
//                contentValues1s.put("age", 19);
//                contentValues1s.put("studentid", UUIDUtils.getUUID(6));
//                getContentResolver().insert(uri1, contentValues1s);
                break;
            case R.id.button2:
                getContentResolver().delete(uri1, "studentid = ?", new String[0]);
                break;
            case R.id.button3:
                ContentValues contentValues = new ContentValues();
                contentValues.put("name", "小明B");
                contentValues.put("sex", "女B");
                contentValues.put("age", 222);
                getContentResolver().update(uri1, contentValues, "0", null);
                break;
            case R.id.button4:
                //获得ContentResolver对象，调用方法
                Cursor cursor = getContentResolver().query(uri1, null, null, null, null);
                studentList.clear();
                while (cursor.moveToNext()) {
                    studentList.add(new Student(
                            cursor.getString(cursor.getColumnIndex("name")),
                            cursor.getString(cursor.getColumnIndex("studentid")),
                            cursor.getString(cursor.getColumnIndex("sex")),
                            cursor.getInt(cursor.getColumnIndex("age"))
                    ));
                }
                Log.e("TTT", studentList.size() + studentList.toString());
                break;
        }
    }
}
